//
//  JPUserProfileView.m
//  JPKY
//
//  Created by Vitas on 2016/11/16.
//  Copyright © 2016年 Vitas. All rights reserved.
//

#import "JPUserProfileView.h"
#import "JPFMDatabaseQueueManager.h"

@interface JPUserProfileView ()

/**
 *  头像
 */
@property (nonatomic, strong) UIImageView *portraitView;

/**
 *  名称
 */
@property (nonatomic, strong) UILabel *nameLabel;

@end

@implementation JPUserProfileView{
     JPFMDatabaseQueueManager *_manager;

}

- (instancetype)init {
    self = [super init];
    if (self) {
        [self setupUI];
        // 接受通知方法
        [self setNotification];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
        // 接受通知方法
        [self setNotification];
    }
    return self;
}

- (void)setNotification{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setupUI) name:@"changeName" object:nil];

}


/**
 *  初始化UI
 */
- (void)setupUI {
    // 获取单例对象
    _manager = [JPFMDatabaseQueueManager sharedManager];
    self.backgroundColor =  [UIColor colorWithRed:87/255.0 green:199/255.0 blue:199/255.0 alpha:1];
    
    [self addSubview:self.portraitView];
    [self addSubview:self.nameLabel];
    
    NSString *name = @"请设置你的昵称";

    NSString *querySQL = @"select name,sex ,IDNumber,tell,height,weight,native,address from t_person where id = 1;";
    [_manager inDatabase:^(FMDatabase *db) {
       
        // 执行查询SQL语句
        FMResultSet *rs = [db executeQuery:querySQL];
        // 遍历结果集,一条一条的取出记录
        while ([rs next]) {
            // 取出对应的值
            if (rs == nil) {
                self.nameLabel.text = name;
            }else{
                self.nameLabel.text =  [rs stringForColumn:@"name"];
            }
        }
        
        
    }];

    
    //判断是否在本地沙盒里面有没有图片
    // NSArray
    NSString *paths = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
    NSString *casesPath = [paths stringByAppendingPathComponent:@"image.data"];
    NSArray *imageArr = [NSKeyedUnarchiver unarchiveObjectWithFile:casesPath];
    if (imageArr.lastObject == nil) {
        self.portraitView.image = [UIImage imageNamed:@"head.jpeg"];

    }else{
        self.portraitView.image = imageArr.lastObject;
        
    }
    
    self.portraitView.layer.cornerRadius = 25;
    self.portraitView.layer.masksToBounds = YES;
    
    // 自动布局
    
    [self.portraitView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.left.equalTo(self).offset(25);
        make.size.mas_equalTo(CGSizeMake(50, 50));
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.portraitView.mas_right).offset(20);
        make.top.equalTo(self.portraitView);
    }];

}

#pragma mark - 懒加载子控件

- (UIImageView *)portraitView {
    if (!_portraitView) {
        _portraitView = [[UIImageView alloc] init];
    }
    
    return _portraitView;
}

- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.textColor = [UIColor whiteColor];
    }
    
    return _nameLabel;
}

@end
